package o;

import android.content.Context;
import android.media.AudioManager;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.text.TextUtils;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.Socket;

public class bmt {
    private static String ˊ;
    private static int ˋ;
    private bmv ॱ;

    public void ˏ(bmy o_bmy, bmx o_bmx) {
        this.ॱ = new bmv();
        this.ॱ.ˎ(o_bmy, o_bmx);
    }

    public bmw ˏ() {
        if (this.ॱ != null) {
            return this.ॱ.ˋ();
        }
        bmr.ॱ.ˎ("DeviceStateKit", "ERROR: try to get network quality without starting sampling");
        return bmw.ॱ;
    }

    public double ˋ() {
        if (this.ॱ != null) {
            return this.ॱ.ˎ();
        }
        return -1.0d;
    }

    public void ˎ() {
        if (this.ॱ != null) {
            this.ॱ.ॱ();
            this.ॱ = null;
        }
    }

    public static boolean ˊ(Context context) {
        return ˋ(context) != 0;
    }

    public static int ˋ(Context context) {
        ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService("connectivity");
        if (connectivityManager == null) {
            bmr.ॱ.ˋ("DeviceStateKit", "connectivityManager is null!");
            return 0;
        }
        NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
        if (activeNetworkInfo == null || !activeNetworkInfo.isConnected()) {
            bmr.ॱ.ˋ("DeviceStateKit", "activeNetworkInfo is null or network is not connected");
            return 0;
        }
        int i;
        int type = activeNetworkInfo.getType();
        if (type == 1 || type == 13) {
            i = 1;
        } else {
            if (type == 0) {
                switch (activeNetworkInfo.getSubtype()) {
                    case 1:
                    case 2:
                    case 4:
                    case 7:
                    case 11:
                        i = 2;
                        break;
                    case 3:
                    case 5:
                    case 6:
                    case 8:
                    case 9:
                    case 10:
                    case 12:
                    case 14:
                    case 15:
                        i = 3;
                        break;
                    case 13:
                        i = 4;
                        break;
                    case 20:
                        i = 5;
                        break;
                }
            }
            i = 0;
        }
        return i;
    }

    public static void ॱ(String str) {
        ˊ = str;
    }

    public static void ˎ(int i) {
        ˋ = i;
    }

    public static boolean ˊ() {
        if (ˊ != null && ˋ != 0) {
            return ˎ(ˊ, ˋ);
        }
        bmr.ॱ.ˏ("DeviceStateKit", "host = " + ˊ + ", port = " + ˋ + ", please init before call. return true for now");
        return true;
    }

    public static boolean ˎ(String str, int i) {
        Socket socket;
        Exception e;
        Throwable th;
        boolean z = false;
        if (!TextUtils.isEmpty(str)) {
            try {
                socket = new Socket();
                try {
                    socket.connect(new InetSocketAddress(str, i), 2000);
                    if (socket.isConnected()) {
                        z = true;
                    }
                    if (socket != null) {
                        try {
                            socket.close();
                        } catch (IOException e2) {
                            bmr.ॱ.ˏ("DeviceStateKit", "Closeable IOException!" + e2.toString());
                        }
                    }
                } catch (Exception e3) {
                    e = e3;
                    try {
                        bmr.ॱ.ˏ("DeviceStateKit", "OTA host Exception!" + e.toString());
                        if (socket != null) {
                            try {
                                socket.close();
                            } catch (IOException e22) {
                                bmr.ॱ.ˏ("DeviceStateKit", "Closeable IOException!" + e22.toString());
                            }
                        }
                        bmr.ॱ.ˋ("DeviceStateKit", str + ":" + i + ", reachable: " + z);
                        return z;
                    } catch (Throwable th2) {
                        th = th2;
                        if (socket != null) {
                            try {
                                socket.close();
                            } catch (IOException e222) {
                                bmr.ॱ.ˏ("DeviceStateKit", "Closeable IOException!" + e222.toString());
                            }
                        }
                        throw th;
                    }
                }
            } catch (Exception e4) {
                e = e4;
                socket = null;
                bmr.ॱ.ˏ("DeviceStateKit", "OTA host Exception!" + e.toString());
                if (socket != null) {
                    socket.close();
                }
                bmr.ॱ.ˋ("DeviceStateKit", str + ":" + i + ", reachable: " + z);
                return z;
            } catch (Throwable th3) {
                th = th3;
                socket = null;
                if (socket != null) {
                    socket.close();
                }
                throw th;
            }
            bmr.ॱ.ˋ("DeviceStateKit", str + ":" + i + ", reachable: " + z);
        }
        return z;
    }

    public static boolean ॱ(Context context) {
        AudioManager audioManager = (AudioManager) context.getSystemService("audio");
        if (audioManager != null) {
            return audioManager.isMusicActive();
        }
        bmr.ॱ.ˋ("DeviceStateKit", "Get AudioManager failed");
        return false;
    }
}
